<template>
    <div class="container">
        <el-row>
            <el-form ref="form" :model="form" label-width="150px">
                <el-form-item label="产品名称：" prop="name" style="width: 500px" :rules="[{ required: true, message: '产品名称不能为空' }]">
                    <el-input v-model="form.name" placeholder="请输入产品名称"></el-input>
                </el-form-item>
                <el-form-item label="产品类型：" prop="productType" style="width: 500px" :rules="[{ required: true, message: '产品类型不能为空' }]">
                    <el-select v-model="form.productType">
                        <el-option label="基金" value="基金"></el-option>
                        <el-option label="政信" value="政信"></el-option>
                        <el-option label="保险" value="保险"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所属产品组合：" prop="combinationName" style="width: 500px" :rules="[{ required: true, message: '所属产品组合不能为空' }]">
                    <el-select v-model="form.combinationName" placeholder="请选择"
                        ><el-option v-for="(item, index) in productCombList" :key="index" :label="item.name" :value="item.name"></el-option
                    ></el-select>
                </el-form-item>
                <el-form-item label="涨跌幅名称：" prop="rangeText" style="width: 500px" :rules="[{ required: true, message: '涨跌幅名称不能为空' }]">
                    <el-input v-model="form.rangeText" placeholder="请输入涨跌幅名称"></el-input>
                </el-form-item>
                <el-form-item label="涨跌幅数据：" prop="rangeIndex" style="width: 500px" :rules="[{ required: true, message: '涨跌幅数据不能为空' }]">
                    <el-input v-model="form.rangeIndex" placeholder="请输入涨跌幅数据："></el-input>
                </el-form-item>
                <el-form-item label="最新净值：" prop="netValueNew" style="width: 500px" :rules="[{ required: true, message: '最新净值不能为空' }]">
                    <el-input v-model="form.netValueNew" placeholder="请输入最新净值"></el-input>
                </el-form-item>
                <el-form-item label="日涨跌：" prop="rangeIndexDay" style="width: 500px" :rules="[{ required: true, message: '日涨跌不能为空' }]">
                    <el-input v-model="form.rangeIndexDay" placeholder="请输入日涨跌"></el-input>
                </el-form-item>
                <el-form-item label="标签：" prop="tags" style="width: 500px" :rules="[{ required: true, message: '标签不能为空' }]">
                    <el-tag :key="tag" v-for="tag in form.tags" closable :disable-transitions="false" @close="handleClose(tag)">
                        {{ tag }}
                    </el-tag>
                    <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
                    </el-input>
                    <el-button v-else class="button-new-tag" size="small" @click="showInput">添加标签</el-button>
                </el-form-item>
                <el-form-item label="描述：" prop="description">
                    <el-input v-model="form.description" placeholder="请输入描述" type="textarea" :rows="6" style="width: 500px"></el-input>
                </el-form-item>
                <el-form-item label="组合介绍：" prop="combinationDesc">
                    <el-input v-model="form.combinationDesc" placeholder="请输入组合介绍" type="textarea" :rows="6" style="width: 500px"></el-input>
                </el-form-item>
                <el-form-item label="收益涨幅：" prop="revenueIndex" style="width: 500px" :rules="[{ required: true, message: '收益涨幅不能为空' }]">
                    <el-input v-model="form.revenueIndex" placeholder="请输入收益涨幅"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit('form')" :loading="loading">保存</el-button>
                </el-form-item>
            </el-form>
        </el-row>
    </div>
</template>

<script>
    import normalFilter from "@/components/NormalFilter";
    import Upload from "@/components/UploadFile";
    import Tinymce from "@/components/Tinymce";
    import { Message } from "element-ui";
    import { add, getDetail, update } from "@/api/product";
    import { getList } from "@/api/productComb";

    export default {
        name: "bannerAdd",
        components: {
            normalFilter,
            Tinymce,
            Upload,
        },
        data() {
            return {
                loading: false,
                id: "",
                tags: [],
                inputVisible: false,
                inputValue: "",
                productCombList: [],
                form: {
                    name: "",
                    combinationName: "",
                    netValueNew: "",
                    rangeIndexDay: "",
                    description: "",
                    combinationDesc: "",
                    rangeIndex: "",
                    revenueIndex: "",
                    rangeText: "",
                    tags: [],
                    productType: "",
                },
            };
        },
        async mounted() {
            let res = await getList({ current: 1, size: 1000 });
            if (res) {
                this.productCombList = res.records;
            }
            let { id } = this.$route.params;
            if (!id) return;
            let info = await getDetail({ id });
            if (info) {
                info["tags"] = info["tags"].split(",");
                this.form = info;
                this.id = id;
            }
        },
        methods: {
            onSubmit(formName) {
                const { validate } = this.$refs[formName];
                validate((valid) => {
                    if (valid) {
                        this._submitData(this.form);
                    }
                });
            },
            async _submitData(params) {
                params = {
                    ...params,
                    tags: this.form.tags.join(","),
                };
                this.loading = true;
                let res = !this.id ? await add(params) : await update({ ...params, id: this.id });
                this.loading = false;
                if (res.code === 1) {
                    Message({
                        message: "保存成功！",
                        type: "success",
                        duration: 1 * 1000,
                    });
                    setTimeout(() => {
                        this.$router.go(-1);
                    }, 1000);
                }
            },
            getFileName(fileUrl) {
                let fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1, fileUrl.length);
                return fileName;
            },
            handleClose(tag) {
                this.form.tags.splice(this.form.tags.indexOf(tag), 1);
            },

            showInput() {
                this.inputVisible = true;
                this.$nextTick((_) => {
                    this.$refs.saveTagInput.$refs.input.focus();
                });
            },

            handleInputConfirm() {
                let inputValue = this.inputValue;
                if (inputValue) {
                    this.form.tags.push(inputValue);
                }
                this.inputVisible = false;
                this.inputValue = "";
            },
        },
    };
</script>

<style lang="scss" scoped>
    .container {
        width: 100%;
        height: 100%;
        padding: 20px;
        .radio-form {
            ::v-deep .el-form-item__error {
                left: 5%;
            }
        }
        .el-tag + .el-tag {
            margin-left: 10px;
        }
        .button-new-tag {
            margin-left: 10px;
            height: 32px;
            line-height: 30px;
            padding-top: 0;
            padding-bottom: 0;
        }
        .input-new-tag {
            width: 90px;
            margin-left: 10px;
            vertical-align: bottom;
        }
    }
</style>
